package com.zhaoyizhe.demo.aspect;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Slf4j
//@Component
@Aspect
public class TimeRecordAspect{
    // 定义切点
    @Pointcut("execution(* com.zhaoyizhe.demo.Controller.*.*(..))")
    public void pointcut(){}

    @SneakyThrows
    @Around(" pointcut() ")
    public Object timeRecord (ProceedingJoinPoint joinPoint) {
        long start = System.currentTimeMillis();
        log.info("TimeRecordAspect.doAround 前");
        Object proceed = joinPoint.proceed();
        log.info("TimeRecordAspect.doAround 后");
        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature()+"执行时间 :"+(end - start)+"ms");
        return proceed;
    }

    @After(" pointcut() ")
    public void doAfter(JoinPoint joinPoint) {
        log.info("TimeRecordAspect.doAfter执行");
    }
}
